package com.dep.service.impl;

import com.dep.common.ResponseData;
import com.dep.dto.req.DepartmentInsertReq;
import com.dep.dto.req.DepartmentReq;
import com.dep.dto.resp.DepartmentAllResp;
import com.dep.dto.resp.DepartmentResp;
import com.dep.entity.Department;
import com.dep.mapper.DepartmentMapper;
import com.dep.service.DepartmentService;
import com.dep.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Resource
    private DepartmentMapper departmentMapper;

    @Override
    public ResponseData<List<DepartmentAllResp>> selectAllDep() {
        List<DepartmentAllResp> allList = departmentMapper.findALl();
        if(allList.isEmpty()){
            return ResponseData.success(allList);
        }

        List<DepartmentAllResp> parentList = new ArrayList<>();
        for(DepartmentAllResp resp:allList){
            if(resp.getParentId() == 0){
                parentList.add(resp);
            }
        }

        for(DepartmentAllResp resp:parentList){
            List<DepartmentAllResp> childList = getChild(resp.getId(), allList);
            resp.setChildList(childList);
        }
        return ResponseData.success(parentList);
    }

    public List<DepartmentAllResp> getChild(Long id,List<DepartmentAllResp> allList){
//子菜单
        List<DepartmentAllResp> childList = new ArrayList<>();
        for (DepartmentAllResp nav : allList) {
            // 遍历所有节点，将所有菜单的父id与传过来的根节点的id比较
            //相等说明：为该根节点的子节点。
            if (nav.getParentId().equals(id)){
                childList.add(nav);
            }
        }
        //递归
        for (DepartmentAllResp nav : childList) {
            nav.setChildList(getChild(nav.getId(), allList));
        }
        //如果节点下没有子节点，返回一个空List（递归退出）
        if (childList.size() == 0 ){
            return new ArrayList<>();
        }
        return childList;
    }

    @Override
    public ResponseData<PageInfo<DepartmentResp>> findDepartmentPage(DepartmentReq req) {
        PageHelper.startPage(req.getPageNo(), req.getPageSize());
        List<DepartmentResp> list = departmentMapper.findPage(req);
        return ResponseData.success(new PageInfo<>(list));
    }

    @Override
    public ResponseData insert(DepartmentInsertReq req) {

        int count = departmentMapper.selectByDepName(req.getDepName());
        if(count > 0){
            return ResponseData.fail("部门名称: "+ req.getDepName()+" 已存在");
        }
        int count2 = departmentMapper.selectByDepNo(req.getDepNo());
        if(count2 > 0){
            return ResponseData.fail("部门编号: "+req.getDepNo()+" 已存在");
        }

        Department department = new Department();

        BeanUtils.copyProperties(req,department);
        department.setEstablishAt(DateUtil.parseDateStr(req.getEstablishAtStr()));
        department.setCreatedAt(new Date());

        int count3 = departmentMapper.insertSelective(department);
        if(count3 > 0){
            return ResponseData.success();
        }

        return ResponseData.fail();
    }
}
